package com.example.klinemqflink.model;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;
import jakarta.annotation.Resource;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.connectors.rabbitmq.RMQDeserializationSchema;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
// 自定义反序列化器
public class RMDeserializationSchema implements RMQDeserializationSchema<MarketData> {

    @Resource
    ObjectMapper objectMapper;

    @Override
    public void deserialize(Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes, RMQCollector<MarketData> out) throws IOException {
        MarketData marketData = new JsonMapper().readValue(bytes, MarketData.class);
        out.collect(marketData);

    }

    @Override
    public boolean isEndOfStream(MarketData nextElement) {
        return false;
    }


    @Override
    public TypeInformation<MarketData> getProducedType() {
        return TypeInformation.of(MarketData.class);
    }

}
